private import Form.BaseForm;
private import System.BootInfo;
private import Input.KeyCode;
private import State.IState;
private import State.InitializeState;
private import Graphics.IGraphics;

private import System.Console;

public class MainForm : BaseForm{
	protected BootInfo _boot;
	
	protected IState _currentState;
	protected IState _lastState;
	protected IGraphics _graphics;
	

	public this( BootInfo b , IGraphics g ){
		super();
		_boot     = b;
		_graphics = g;
	} //
	public this( char[] caption , BootInfo b , IGraphics g ){
		super( caption );
		_boot     = b;
		_graphics = g;
	} //
	
	public bool init(){
		_currentState = new InitializeState( _graphics );
		_lastState    = _currentState;
		_currentState.call( null );
		return true;
	} //
	public bool close(){
		Console.wn = "Good bye!";
		return true;
	} //

	public bool onExit(){
		return setQuit();
	} //

	/**
	*	ここが実質のメインループ
	*/
	public bool onTimer(){
		if ( _quitFlag ) return false;
		action();
		draw();
		return true;
	} //
	
	
	public bool action(){
		_currentState = _currentState.action();
		if( _currentState is null ) setQuit();
		return true;
	} //
	
	public bool draw(){
		_lastState.draw( _graphics );
		_lastState = _currentState;
		return true;
	} //
	
	public bool onMouseMove( int x , int y , int button){
		if ( _boot.isScreensaver ) return setQuit();
		return _currentState.onMouseMove( x , y , button);
	} //

	public bool onMouseDown( int x , int y , int button){
		if ( _boot.isScreensaver ) return setQuit();
		return _currentState.onMouseDown( x , y , button);
	} //

	public bool onMouseUp( int x , int y , int button){
		return _currentState.onMouseUp( x , y , button);
	} //

	public bool onMouseWheel( int x , int y , int deltaY , int deltaX = 0 ){
		if ( _boot.isScreensaver ) return setQuit();
		return _currentState.onMouseWheel( x , y , deltaY );
	} //
	
	public bool onKeyDown( int keyCode ){
		if ( _boot.isScreensaver ) return setQuit();
		if( keyCode == KeyCode.ESCAPE ) return setQuit(); //{ _closeFlag = true; return false;}
		return _currentState.onKeyDown( keyCode );
	} //

	public bool onKeyUp( int keyCode ){
		return _currentState.onKeyUp( keyCode );
	} //

	public bool onJoyAxis( int id , int axis , int delta ){
		if ( _boot.isScreensaver ) return setQuit();
		return _currentState.onJoyAxis( id , axis , delta );
	} //
	
	public bool onJoyButtonDown( int id , int button ){
		if ( _boot.isScreensaver ) return setQuit();
		return _currentState.onJoyButtonDown( id , button );
	} //

	public bool onJoyButtonUp( int id , int button ){
		return _currentState.onJoyButtonUp( id , button );
	} //

} //

